﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls.RichTextBox.TextSearch;
using Telerik.WinControls.UI;

namespace LogViewApp
{
    public partial class LogViewSForm : Telerik.WinControls.UI.ShapedForm
    {
        //private BackgroundWorker worker;
        private string sDetail = string.Empty;

        public LogViewSForm()
        {
            InitializeComponent();
            Init();  
        }

        private void Init()
        {
            this.Load += LogViewSForm_Load;
            this.rlcloglist.SelectedValueChanged += rlcloglist_SelectedValueChanged;
            this.KeyDown += LogViewSForm_KeyDown;
        }

        void LogViewSForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F5)
            {
                BindData();
            }
        }

        void rlcloglist_SelectedValueChanged(object sender, EventArgs e)
        {
            this.rtbcText.Text = string.Empty;
            this.rtbcText.Update();
            ShowInfo();
        }

        private void LogViewSForm_Load(object sender, EventArgs e)
        { 
            BindData();
        }

        private void BindData()
        {
            DirectoryInfo di = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log"));
            if (di.Exists)
            {
                FileInfo[] fis = di.GetFiles();
                this.rlcloglist.Items.Clear();
                foreach (FileInfo fi in fis)
                    this.rlcloglist.Items.Add(new RadListDataItem(fi.Name, fi.FullName));
            }
        }

        private void ShowInfo()
        {
            RadListDataItem item = this.rlcloglist.SelectedItem; 
            if (item != null)
            {
                FileInfo fi = new FileInfo(item.Value.ToString());
                using (FileStream fs = fi.OpenRead())
                {
                    StreamReader sr = new StreamReader(fs, Encoding.Default);
                    string str;
                    int i = 1;
                    while ((str = sr.ReadLine()) != null)
                    { 
                        this.rtbcText.AppendText(string.Format("第{0}行   {1} \r\n", i++, str));
                        this.rtbcText.AppendText("\r\n");
                        this.rtbcText.Update(); 
                    }
                    //sDetail = sr.ReadToEnd();
                } 
            }
        } 

        private void delrmi_Click(object sender, EventArgs e)
        {
            if (this.rlcloglist.SelectedItem != null)
            {
                if (MessageBox.Show(this, "你确定要删除日志文件？","",MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    RadListDataItem item = this.rlcloglist.SelectedItem;
                    FileInfo fi = new FileInfo(item.Value.ToString());
                    try
                    {
                        fi.Delete();
                        this.rlcloglist.Items.Remove(item);
                    }
                    catch 
                    {
                        MessageBox.Show("删除失败,当日日志不能删除");
                    }
                } 
             
            }
        } 
    }
}
